<template>
  <div style="padding-bottom: 10px" class="dialog-site-select">
    <el-button style="width: 100%" @click="clickDialogTableVisible">
      选择指定网站
      【<el-image
        style="width: 15px; height: 15px"
        :src="siteInfo.avatar"
        fit="cover"></el-image>
      {{siteInfo.name}}】&nbsp;&nbsp;
      <i class="el-icon-caret-bottom"></i>
    </el-button>

    <el-dialog width="90%" custom-class="dialog-site-select" title="站点列表" :visible.sync="dialogTableVisible">
      <div class="filter-container">
        <el-input v-model="listQuery.name" placeholder="站点名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
          搜索
        </el-button>
      </div>
      <div class="select-site-div">
        <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
          <el-table-column align="center" label="ID" width="180">
            <template slot-scope="{row}">
              <span>{{ row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="封面" width="120">
            <template slot-scope="{row}">
              <el-image :src="row.avatar" style="width: 50px;height: 50px" :preview-src-list="[row.avatar]">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline" style="font-size: 40px"></i>
                </div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column align="center" label="名称">
            <template slot-scope="{row}">
              <span>{{ row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column class-name="status-col" label="状态" width="110">
            <template slot-scope="{row}">
              <span>{{ row.status_text }}</span>
            </template>
          </el-table-column>

          <el-table-column label="创建时间" width="160">
            <template slot-scope="{row}">
              <span>{{ row.create_time }}</span>
            </template>
          </el-table-column>
          <el-table-column class-name="status-col" label="更新时间" width="160">
            <template slot-scope="{row}">
              <span>{{ row.update_time }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button type="primary" plain size="small" icon="el-icon-right" @click="clickSelect(scope.row)">
                立即选择
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.per_page" @pagination="getList" />
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { getSiteList } from '@/api/site'
import Pagination from '@/components/Pagination'
export default {
  name: 'SecletSite',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        per_page: 10,
        title: undefined
      },
      dialogTableVisible: false,
      siteInfo: {
        name: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    clickDialogTableVisible() {
      this.dialogTableVisible = true
      this.getList()
    },
    getList() {
      this.listLoading = true
      getSiteList(this.listQuery).then(response => {
        if (response.total && !this.siteInfo.name) {
          this.siteInfo = response.data[0]
          this.emitSelectSite(this.siteInfo)
        }

        this.list = response.data
        this.total = response.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    clickSelect(data) {
      this.siteInfo = data
      this.dialogTableVisible = false

      this.emitSelectSite(data)
    },
    emitSelectSite(data) {
      this.$emit('emitSelectSite', data)
    }
  }
}
</script>
<style>
  .dialog-site-select{
    width: 90%;
    height: 92%;
    margin-top: 2% !important;
    position: relative;
    .el-dialog__body {
      width: 100%;
      padding: 10px;
      padding-top: 0px;
      height: 90%;
      overflow-y: scroll;
    }
    .el-dialog__header {
      padding-bottom: 0px;
     }
  }
</style>

